package com.diandireader.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @author mayh
 *
 */
public class StringHelper {

	//获得指定长度的文本，其余的以...代替
	public static String getShortString(String str,int length){
		if(str==null||"".equals(str)){
			str = "未命名";
		}else {
			//去掉后缀
			int position = str.lastIndexOf(".");
			str = str.substring(0, position);
			if(str.length()>length){
				str = str.substring(0,length-1)+"..";
			}
		}
		return str;
	}
	
	//format date to string
	public static String dateToString(Date date,String pattern){
		if(pattern==null||pattern.equals(""))
			pattern = "yyyy-MM-dd HH:mm";
		if(date==null)
			date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	
	//substring from specified string
	public static String subString(String str,int counts){
		if(str!=null&&!"".equals(str)&&str.length()>counts){
			str = str.trim().substring(0, counts);
		}
		return str;
	}
	
}
